///////////////////////////////////////////////////////////////////////
// File:        PrinterSink.cpp
// Description: 
// Author:      Philip Kommnick
// Created:     Thu September 17 12:26:39 CEST 2009
//
// This program is open source. For license terms, see the LICENSE file.
//
///////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "PrinterSink.h"
#include "resource.h"

PrinterSink::PrinterSink(void) : rememberPrinter(false)
{
}

PrinterSink::~PrinterSink(void)
{
}

HRESULT PrinterSink::DisplayPrintPropertySheet(HWND hWnd)
{
    HRESULT hResult;
    LPPRINTDLGEX pPDX = NULL;
    LPPRINTPAGERANGE pPageRanges = NULL;

    // Allocate the PRINTDLGEX structure.
    pPDX = (LPPRINTDLGEX)GlobalAlloc(GPTR, sizeof(PRINTDLGEX));
    if (!pPDX)
        return E_OUTOFMEMORY;

    // Allocate an array of PRINTPAGERANGE structures.
    pPageRanges = (LPPRINTPAGERANGE) GlobalAlloc(GPTR, 10 * sizeof(PRINTPAGERANGE));
    if (!pPageRanges)
        return E_OUTOFMEMORY;

    //  Initialize the PRINTDLGEX structure.
    pPDX->lStructSize = sizeof(PRINTDLGEX);
    pPDX->hwndOwner = hWnd;
    pPDX->hDevMode = NULL;
    pPDX->hDevNames = NULL;
    pPDX->hDC = NULL;
    pPDX->Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | PD_COLLATE;
    pPDX->Flags2 = 0;
    pPDX->ExclusionFlags = 0;
    pPDX->nPageRanges = 0;
    pPDX->nMaxPageRanges = 10;
    pPDX->lpPageRanges = pPageRanges;
    pPDX->nMinPage = 1;
    pPDX->nMaxPage = 1000;
    pPDX->nCopies = 1;
    pPDX->hInstance = 0;
    pPDX->lpPrintTemplateName = NULL;
    pPDX->lpCallback = NULL;
    pPDX->nPropertyPages = 0;
    pPDX->lphPropertyPages = NULL;
    pPDX->nStartPage = START_PAGE_GENERAL;
    pPDX->dwResultAction = 0;
    
    //  Invoke the Print property sheet.
    
    hResult = PrintDlgEx(pPDX);

    if ((hResult == S_OK) && pPDX->dwResultAction == PD_RESULT_PRINT) 
    {
        // User clicked the Print button, so
        // use the DC and other information returned in the 
        // PRINTDLGEX structure to print the document.
    }

    if (pPDX->hDC != NULL) 
        DeleteDC(pPDX->hDC);
    if (pPDX->hDevMode != NULL) 
        GlobalFree(pPDX->hDevMode); 
    if (pPDX->hDevNames != NULL) 
        GlobalFree(pPDX->hDevNames); 

    return hResult;
}

void PrinterSink::Output(Gdiplus::Bitmap &bmp)
{
	if (rememberPrinter || SelectPrinter()) {
		DisplayPrintPropertySheet(MainWin::hWnd);
		throw std::exception("druckt ned");
	}
}

bool PrinterSink::SelectPrinter()
{
	//const int offset = 1000;
	//DWORD prBufSize, prCnt;
	//EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, NULL, 0, &prBufSize, &prCnt);
	//if (prBufSize==0)
	//	return false;
	//int returnButton = IDCANCEL;
	//PRINTER_INFO_4 *printerInfos = new PRINTER_INFO_4[prBufSize/sizeof(PRINTER_INFO_4)];
	//if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 4, (LPBYTE)printerInfos, prBufSize, &prBufSize, &prCnt)) {
	//	TASKDIALOG_BUTTON *printerButtons = new TASKDIALOG_BUTTON[prCnt];
	//	std::vector<std::wstring> buttonStrings;
	//	for (DWORD i=0; i<prCnt; i++) {
	//		printerButtons[i].nButtonID = offset+i;
	//		buttonStrings.push_back(printerInfos[i].pPrinterName);
	//		if (printerInfos[i].pServerName) {
	//			buttonStrings[i] += L"\n on ";
	//			buttonStrings[i] += printerInfos[i].pServerName;
	//		}
	//		printerButtons[i].pszButtonText = buttonStrings[i].c_str();
	//	}
	//	TASKDIALOGCONFIG tdcSelectPrinter = {sizeof(TASKDIALOGCONFIG)};
	//	tdcSelectPrinter.hwndParent = MainWin::hWnd;
	//	tdcSelectPrinter.hInstance = tpInstance;
	//	tdcSelectPrinter.dwFlags = TDF_CAN_BE_MINIMIZED | TDF_USE_COMMAND_LINKS | TDF_ALLOW_DIALOG_CANCELLATION;
	//	tdcSelectPrinter.pButtons = printerButtons;
	//	tdcSelectPrinter.cButtons = prCnt;
	//	tdcSelectPrinter.pszWindowTitle = MAKEINTRESOURCE(IDS_APPNAME);
	//	tdcSelectPrinter.pszMainIcon = MAKEINTRESOURCE(IDI_PRINT);
	//	tdcSelectPrinter.pszMainInstruction = MAKEINTRESOURCE(IDS_PRINTSELECTMAIN);
	//	tdcSelectPrinter.pszContent = MAKEINTRESOURCE(IDS_PRINTSELECTCONT);
	//	tdcSelectPrinter.pszVerificationText = MAKEINTRESOURCE(IDS_TDCREMEMBER);
	//	int selectedPrinter, chckBoxState;
	//	TaskDialogIndirect(&tdcSelectPrinter, &returnButton, &selectedPrinter, &chckBoxState);
	//	if (returnButton >= offset) {
	//		rememberPrinter = (chckBoxState==TRUE);
	//		lastPrinter = printerInfos[returnButton-offset].pPrinterName;
	//	}
	//	delete[] printerButtons;
	//	buttonStrings.clear();
	//}
	//delete[] printerInfos;
	//return returnButton >= offset;
	return true;
}
